﻿Public Class frmQuotation
    Public itemid = 0
    Public total = 0
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        UI.FormatDataGrid(dgvlist)
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        Dim row As DataGridViewRow

        If Not (dgvlist.SelectedRows.Count < 1) Then
            row = dgvlist.SelectedRows(0)
            lblsubtotal.Text = CInt(Val(lblsubtotal.Text) - row.Cells("amount").Value)
            lblTotal.Text = CInt(Val(lblsubtotal.Text) - (Val(lblsubtotal.Text) * (Val(txttotaldisc.Value) / 100)))
            Me.dgvlist.Rows.Remove(row)
            txtowe.Text = Val(lblTotal.Text) - Val(txtamount.Text)
            ' Else
            ' MessageBox.Show("ABC")
        End If

    End Sub

    Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
        Dim row As DataGridViewRow
        row = dgvlist.SelectedRows(0)
        Me.dgvlist.Rows.Clear()
        lblsubtotal.Text = "0"
        lblTotal.Text = "0"
        txtowe.Text = "0"
    End Sub

    Private Sub txtamount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtamount.TextChanged
        txtowe.Text = Val(lblTotal.Text) - Val(txtamount.Text)
    End Sub


    Private Sub txtqty_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtqty.KeyDown
        If e.KeyCode = Keys.Enter Then
            addRow()
            txtcode.Text = ""
            txtcode.Focus()
        End If
    End Sub

    Sub clearitem()
        txtitemname.Text = "N/A"
        txtqty.Text = "1"
        txtprice.Text = "0"
        txtdiscount.Value = 0
        itemid = 0
        total = 0
        picitem.Image = Nothing
    End Sub

    Private Sub addRow()
        If itemid <> 0 Then
            Dim i = Me.dgvlist.Rows.Add
            Dim row = Me.dgvlist.Rows(i)
            total = (Val(txtqty.Text) * Val(txtprice.Text))
            total = CInt(total - ((total * Val(txtdiscount.Value)) / 100))
            row.Cells("item_id").Value = itemid
            row.Cells("item_name").Value = txtitemname.Text
            row.Cells("item_price").Value = txtprice.Text
            row.Cells("item_qty").Value = txtqty.Text
            row.Cells("amount").Value = total
            row.Cells("item_discount").Value = txtdiscount.Value
            row.Cells("subamount").Value = Val(txtqty.Text) * Val(txtprice.Text)
            lblsubtotal.Text = CInt(Val(lblsubtotal.Text) + total)

            lblTotal.Text = CInt(Val(lblsubtotal.Text) - (Val(lblsubtotal.Text) * (Val(txttotaldisc.Value) / 100)))

            txtowe.Text = Val(lblTotal.Text) - Val(txtamount.Text)
            clearitem()
        End If

    End Sub
    Private Sub txttotaldisc_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttotaldisc.ValueChanged
        lblTotal.Text = CInt(Val(lblsubtotal.Text) - (Val(lblsubtotal.Text) * (Val(txttotaldisc.Value) / 100)))
    End Sub

    Private Sub txtprice_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtprice.KeyDown
        If e.KeyCode = Keys.Enter Then
            addRow()
            txtcode.Text = ""
            txtcode.Focus()
        End If
    End Sub

    Private Sub txtdiscount_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtdiscount.KeyDown
        If e.KeyCode = Keys.Enter Then
            addRow()
            txtcode.Text = ""
            txtcode.Focus()
        End If
    End Sub

    Private Sub dgvlist_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dgvlist.RowPrePaint
        If e.RowIndex >= 0 Then
            Me.dgvlist.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
        End If
    End Sub

    Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click
        Dim frm As New frmViewProQuote
        frm.ShowDialog()
    End Sub


End Class